package com.doctor.web.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doctor.common.core.domain.entity.SysUser;
import com.doctor.common.exception.ServiceException;
import com.doctor.common.utils.DateUtils;
import com.doctor.system.mapper.SysUserMapper;
import com.doctor.web.domain.vo.CommentsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.doctor.web.mapper.ActivityReservationMapper;
import com.doctor.web.domain.ActivityReservation;
import com.doctor.web.service.IActivityReservationService;

/**
 * 活动预约Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-24
 */
@Service
public class ActivityReservationServiceImpl extends ServiceImpl<ActivityReservationMapper, ActivityReservation> implements IActivityReservationService {
    @Autowired
    private ActivityReservationMapper activityReservationMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询活动预约
     * 
     * @param id 活动预约主键
     * @return 活动预约
     */
    @Override
    public ActivityReservation selectActivityReservationById(Long id)
    {
        return activityReservationMapper.selectActivityReservationById(id);
    }

    /**
     * 查询活动预约列表
     * 
     * @param activityReservation 活动预约
     * @return 活动预约
     */
    @Override
    public List<ActivityReservation> selectActivityReservationList(ActivityReservation activityReservation)
    {
        return activityReservationMapper.selectActivityReservationList(activityReservation);
    }

    /**
     * 新增活动预约
     * 
     * @param activityReservation 活动预约
     * @return 结果
     */
    @Override
    public int insertActivityReservation(ActivityReservation activityReservation)
    {
        activityReservation.setCreateTime(DateUtils.getNowDate());
        return activityReservationMapper.insertActivityReservation(activityReservation);
    }

    /**
     * 修改活动预约
     * 
     * @param activityReservation 活动预约
     * @return 结果
     */
    @Override
    public int updateActivityReservation(ActivityReservation activityReservation)
    {
        activityReservation.setUpdateTime(DateUtils.getNowDate());
        return activityReservationMapper.updateActivityReservation(activityReservation);
    }

    /**
     * 批量删除活动预约
     * 
     * @param ids 需要删除的活动预约主键
     * @return 结果
     */
    @Override
    public int deleteActivityReservationByIds(Long[] ids)
    {
        return activityReservationMapper.deleteActivityReservationByIds(ids);
    }

    /**
     * 删除活动预约信息
     * 
     * @param id 活动预约主键
     * @return 结果
     */
    @Override
    public int deleteActivityReservationById(Long id)
    {
        return activityReservationMapper.deleteActivityReservationById(id);
    }

    @Override
    public List<CommentsVo> getComments(Long activityId) {
        List<CommentsVo> commentsVoList = new ArrayList<>();

        // 查询所有的活动预约记录
        List<ActivityReservation> activityReservations = activityReservationMapper.selectActivityReservationListByActivityId(activityId);

        for (ActivityReservation reservation : activityReservations) {
            Long userId = reservation.getUserId();
            // 通过 userId 查询对应的 SysUser 记录
            SysUser sysUser = sysUserMapper.selectUserById(userId);

            if (sysUser == null) {
                throw new ServiceException("用户信息不存在");
            }

            CommentsVo commentsVo = new CommentsVo();
            if (reservation.getComment()!= null){
                commentsVo.setScore(reservation.getScore());
                commentsVo.setComment(reservation.getComment());
                commentsVo.setCommentTime(reservation.getCommentTime());

                commentsVo.setUserId(userId);
                commentsVo.setNickName(sysUser.getNickName());
                commentsVo.setAvatar(sysUser.getAvatar());

                commentsVoList.add(commentsVo);
            }
        }

        return commentsVoList;
    }
}
